<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/4/14
 * Time: 17:09
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\index\controller;

use app\BaseController;
use app\common\lib\Show;
use app\common\model\DiyHome;

class Diy extends BaseController
{
    public function getDiyByJsonList()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id === 0) {
            return Show::json_diy(0,'配置错误');
        }
        //获取内容
        try {
            $row = (new DiyHome())->getBaseById($id);
        }catch (\Exception $e){
            return Show::json_diy(0,"内部错误");
        }
        if($row) {
            $lists = $row->toArray();
            $lists['content'] = json_decode($lists['content'],true);
            return Show::json_diy(1, "ok", $lists);
        }else{
            return Show::json_diy(0,'暂无数据');
        }
    }
    public function saveDiyData()
    {
        $id = $this->request->param('id', 0, 'intval');
        $content = $this->request->param('data', '', 'trim');
        $data = [
            "id" => $id,
            "content" => $content
        ];
        try {
            $res = (new DiyHome())->updateDataSave($id, [
                "content" => json_encode($data),
                "update_time" => time()
            ]);
        } catch (\Exception $exception) {
            return Show::json_diy(0, $exception->getMessage());
        }
        if ($res) {
            return Show::json_diy(1, "操作成功");
        } else {
            return Show::json_diy(0, "更新失败");
        }

    }
}